include("shared.lua")

module("Block", package.seeall )

CreateClientConVar("nc_class", tostring(SCOUT), true, true )

local HealthWidth = 150
local RepairingBlock

usermessage.Hook("Block", function( um )

	local Type = um:ReadChar()
	
	if Type == 0 then
		
		local RepairEntity = um:ReadEntity()
		local StartTime = um:ReadLong()
		local State = um:ReadBool()
		
		RepairingBlock = {
			Block = RepairEntity,
			StartTime = StartTime,
			StartState = State,
		}
	
	elseif Type == 1 then
	
		RepairingBlock = nil
	
	end


end )

hook.Add("HUDPaint", "DrawBlockHealth", function()

	local State = Game.State()

	if State == Game.FIGHT && ActiveGun( LocalPlayer() ) then
	
		local Block = AimOnBlock( LocalPlayer() )
		local X, Y = ScrW() / 2 - HealthWidth / 2, ScrH() / 2 - 50
		local W, H = HealthWidth, 30
		
		if Block then
			
			surface.SetDrawColor( 0, 255, 0, 100)
			surface.DrawRect(X, Y, W * Block:HealthFrac(), H)
			
			surface.SetDrawColor( 0, 0, 0, 255)
			surface.DrawOutlinedRect(X, Y, W, H)
			
			surface.SetDrawColor( 0, 0, 0, 255)
			surface.DrawOutlinedRect(X - 2, Y - 2 , W + 4 , H + 4)
		
		end
		
		if RepairingBlock then
			
			local Collapsed = CurTime() - RepairingBlock.StartTime
			local Total = GetWeldTime( LocalPlayer(), RepairingBlock.Block, RepairingBlock.StartState )
			
			
			surface.SetDrawColor( 100, 0, 0, 100)
			surface.DrawRect(X, Y, W * (Collapsed/Total) , 10)
			
		end
	
	elseif State == Game.BUILD then
	
		local Block = AimOnBlock( LocalPlayer() )
		
		if Block then
		
			if Block:AllowMotion() then
				surface.SetDrawColor( 255, 0, 0, 50)
			else
				surface.SetDrawColor( 0, 255, 0, 50)
			end
		
			surface.DrawRect( 	
				ScrW() / 2 - 5,
				ScrH() / 2 - 5 + 50,
				10,
				10
			)
		
		end

	end

end )